/**
 * Copyright 2007 Melange.
 *
 * This file is part of PHP-MELANGE.
 *
 * PHP-MELANGE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHP-MELANGE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP-MELANGE; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category    Melange
 * @package     php-melange
 * @subpackage  cms
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 *
 */

function to(url) {
    var base = document.getElementsByTagName("base")[0].getAttribute("href");
    document.location.href=base + url;
}
function moveup(id, form) {
    move(id, form, "moveup");
}
function movedown(id, form) {
    move(id, form, "movedown");
}
function move(id, form, direction) {
    uncheckAll(form.getAttribute("id"));
    check1(form.getAttribute("id"), id);
    form.elements["action"].value=direction;
//    form.submit();
}
function del(id, name) {
    uncheckAll(name);
    check1(name, id);
    var form = document.getElementById(name);
    form.elements["action"].value="delete";
    if(form.onsubmit())
        form.submit();
}
function confirmDelete(msg, form) {
    if(form.elements['action'].value == "delete" && !confirm(msg)) {
        return false;
    }
    return true;
}
function popup(name, url, attr) {
    var base = document.getElementsByTagName("base")[0].getAttribute("href");
    window.open(name, base + url, attr);
}
function more(form) {
    document.getElementById(form).elements["more"].value="1";
    document.getElementById(form).submit();
}
function setOrder(form, field) {
    document.getElementById(form).elements["order"].value = field;
    document.getElementById(form).elements["direction"].value = document.getElementById(form).elements["direction"].value == "desc" ? "asc" : "desc";
    document.getElementById(form).submit();
}
function setPage(form, page) {
    document.getElementById(form).elements["page"].value = page;
    document.getElementById(form).submit();
}
function checkAll(form, name) {
    check(form, true, name)
}
function uncheckAll(form, name) {
    check(form, false, name)
}
function check(form, state, name) {

    var checkboxes = document.getElementById(form).getElementsByTagName("input");
    for(var i=0; i<checkboxes.length; i++) {
        if(checkboxes[i].type == "checkbox") {
            if(name == null || checkboxes[i].name.indexOf(name) > -1) {
                checkboxes[i].checked = state;
                checkboxes[i].focus();
            }
        }
    }
}
function check1(form, value) {
    var checkboxes = document.getElementById(form).getElementsByTagName("input");
    for(var i=0; i<checkboxes.length; i++) {

        if(checkboxes[i].type == "checkbox" && checkboxes[i].value == value) {
            checkboxes[i].checked = true;
            checkboxes[i].focus();
        }
    }
}
function query(formId) {
    document.getElementById(formId).query.focus();
}
function clean(formId) {
    document.getElementById(formId).query.value = "";
    document.getElementById(formId).filter.selectedIndex = 0;
    document.getElementById(formId).results.selectedIndex = 1;
    if(document.getElementById(formId).language)
        document.getElementById(formId).language.selectedIndex = 0;
    document.getElementById(formId).submit();
}

function ito(input) {
    window.scrollTo(findPosX(input), findPosY(input));
}
function ifocus(input) {
    input.originalClassName = input.className;
    input.className="focus";
}
function iblur(input) {
    input.className=input.originalClassName;
}

function findPosY(obj) {
    var curtop = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curtop += obj.offsetTop
            obj = obj.offsetParent;
        }
    } else if (obj.y)
        curtop += obj.y;
    return curtop;
}
function findPosX(obj) {
    var curleft = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            curleft += obj.offsetLeft
            obj = obj.offsetParent;
        }
    } else if (obj.x)
        curleft += obj.x;
    return curleft;
}

// HIGHLIGHT ELEMENTS WITH THE CTRL.. CLASS

function ready() {

    var elements = new Array(
        document.getElementsByTagName("input"),
        document.getElementsByTagName("textarea"),
        document.getElementsByTagName("select")
    );

    for(var i=0; i<elements.length; i++) {
        for(var j=0; j<elements[i].length; j++) {
            if(elements[i][j].getAttribute("type") != "submit" &&
               elements[i][j].getAttribute("type") != "button") {
                elements[i][j].onfocus = function() {
                    var parent = this;
                    while(parent) {
                        if(parent.className && parent.className.indexOf("ctrl") >= 0) {
                            parent.className = parent.className + " focused";
                            this.highlight = parent;
                            break;
                        }
                        parent = parent.parentNode;
                    }
                };
                elements[i][j].onblur = function() {
                    if(this.highlight)
                        this.highlight.className = this.highlight.className.substring(0,
                            this.highlight.className.lastIndexOf(" "));
                };
            }
        }
    }
}

function init() {

    var b = navigator.userAgent.toLowerCase();
    var safariTimer = null;

    // Figure out what browser is being used
    var browser = {
        safari: /webkit/.test(b),
        opera: /opera/.test(b),
        msie: /msie/.test(b) && !/opera/.test(b),
        mozilla: /mozilla/.test(b) && !/(compatible|webkit)/.test(b)
    };

    if(browser.mozilla) {

        document.addEventListener("DOMContentLoaded", ready, false);

    } else if ( browser.msie ) {

        // Only works if you document.write() it
        document.write("<scr" + "ipt id=__ie_init defer=true " +
            "src=//:><\/script>");

        // Use the defer script hack
        var script = document.getElementById("__ie_init");

        // script does not exist if jQuery is loaded dynamically
        if ( script )
            script.onreadystatechange = function() {
                if ( this.readyState != "complete" ) return;
                this.parentNode.removeChild( this );
                ready();
            };

        // Clear from memory
        script = null;

    // If Safari  is used
    } else if ( browser.safari )

        // Continually check to see if the document.readyState is valid
        safariTimer = setInterval(function() {
            // loaded and complete are both valid states
            if ( document.readyState == "loaded" ||
                document.readyState == "complete" ) {

                // If either one are found, remove the timer
                clearInterval( safariTimer );
                safariTimer = null;

                // and execute any waiting functions
                ready();
            }
        }, 10);

    // A fallback to window.onload, that will always work
    window.onload =  ready;
}

init();